package databasetest;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDesktopPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
//가똒현형
public class DatabaseTest extends JFrame {
	
	String drive = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@192.168.10.138:1521:XE"; // 오라클 주소 연결
	String db_user = "hr"; // 데이터베이스의 유저
	String db_pwd = "hr"; // 데이터베이스의 암호
	
	
	Connection conn;
	
	Insert insert;
	Update update;
	Delete delete;
	Select select;
	
	private JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnMainMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenuItem mntmNewMenuItem_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DatabaseTest frame = new DatabaseTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DatabaseTest() {
		setTitle("데이터베이스 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 990, 700);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		// 데이터베이스의 로딩과 연결을 하기위한 준비!!!
		try{
			//데이터베이스 연결 테스트
			// 1) 드라이버 로딩
			Class.forName(drive).newInstance();
			JOptionPane.showMessageDialog(null, "드라이버가 로딩되었습니다.");
			
			// 2) Connection 객체 생성
			conn = DriverManager.getConnection(url, db_user, db_pwd);
			JOptionPane.showMessageDialog(null, "테이터베이스가 정상적으로 연결되었습니다.");
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	
	public void openUpdate(){
		if(update == null){
			update = new Update(conn, this);
			contentPane.add(update);
		}
		update.setVisible(true);
	}
	
	public void openDelete(){
		if(delete == null){
			delete = new Delete(conn, this);
			contentPane.add(delete);
		}
		update.setVisible(true);
	}
	
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnMainMenu());
		}
		return menuBar;
	}
	private JMenu getMnMainMenu() {
		if (mnMainMenu == null) {
			mnMainMenu = new JMenu("New menu");
			mnMainMenu.add(getMntmNewMenuItem());
			mnMainMenu.add(getMntmNewMenuItem_1());
			mnMainMenu.add(getMntmNewMenuItem_2());
			mnMainMenu.add(getMntmNewMenuItem_3());
		}
		return mnMainMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("입력");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(insert== null){
						insert = new Insert(conn, DatabaseTest.this);
						contentPane.add(insert);
					}
					insert.setVisible(true);
					
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("수정");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openUpdate();
					update.setVisible(true);
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("삭제");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(delete == null){
						delete = new Delete(conn, DatabaseTest.this);
						contentPane.add(delete);
					}
					delete.setVisible(true);
				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("조회");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(select == null){
						select = new Select(conn, DatabaseTest.this);
						contentPane.add(select);
					}
					
					select.setVisible(true);
				}
			});
		}
		return mntmNewMenuItem_3;
	}
}
